var postsData = require('../../../data/posts-data.js')
var app = getApp();

Page({
  data: {
    isPlayingMusic: false
  },
  onLoad: function (option) {
    var postId = option.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData
    })
    // 使用缓存实现文章收藏功能
    var postsCollected = wx.getStorageSync('posts_collected');
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      this.setData({
        collected: postCollected
      })
    } else {
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected);
    }

    if (app.globalData.g_isPlayingMusic && app.globalData.g_iscurrentMusicPostId == postId) {
      this.setData({
        isPlayingMusic: true
      })
    }
    this.setMusicMonitor();
  },

  setMusicMonitor: function () {
    var _this = this;
    // 监听音乐播放
    wx.onBackgroundAudioPlay(function (event) {
      _this.setData({
        isPlayingMusic: true
      })
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_iscurrentMusicPostId = _this.data.currentPostId;
    });
    // 监听音乐暂停
    wx.onBackgroundAudioPause(function (event) {
      _this.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_iscurrentMusicPostId = false;
    })
  },

  // 缓存Storage的基本用法
  onCollectionTap: function (event) {
    var postsCollected = wx.getStorageSync('posts_collected');
    var postCollected = postsCollected[this.data.currentPostId];
    // 收藏变成未收藏，未收藏变成收藏
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    this.showToast(postsCollected, postCollected);
  },

  // 交互反馈 wx.showToast
  showToast: function (postsCollected, postCollected) {
    // 更新是否收藏的缓存值
    wx.setStorageSync('posts_collected', postsCollected);
    // 更新数据绑定，切换图片
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? '收藏成功' : '取消收藏',
      icon: 'success',
      duration: 1000
    })
  },

  onShareTap: function (event) {
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ],
      _this = this;
    // 交互反馈 wx.showActionSheet
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function (res) {
        _this.showModal(itemList, res);
      }
    })
  },

  // 操作反馈 wx.showModal
  showModal: function (itemList, res) {
    wx.showModal({
      // res.tapIndex 数组元素的序号，从0开始
      title: '用户' + itemList[res.tapIndex],
      // res.cancel 是否点击了取消按钮
      content: '用户是否取消？' + res.cancel + "现在无法实现",
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#333',
      confirmText: '确认',
      confirmColor: '#405f80'
    })
  },

  onMusicTap: function (event) {
    var currentPostId = this.data.currentPostId,
      postData = postsData.postList[currentPostId];
    var isPlayMusic = this.data.isPlayingMusic;
    if (isPlayMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      })
    } else {
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImgUrl: postData.music.coverImg
      })
      this.setData({
        isPlayingMusic: true
      })
    }
  }
})